<?php


namespace App\Controller\Test;


use App\Controller\BaseController;
use App\Utils\MemcacheClient;
use App\Utils\MongoDBClient;
use App\Utils\RedisClient;

class CacheDemoController extends BaseController
{

    public function memcache()
    {
        $user = $this->request->post('name', 'Hyperf');
        $id = random_int(1, 1000);
        $memcacheClient = MemcacheClient::getInstance();
        $set = $memcacheClient->set("memcache_test_" . $id, ['id' => $id, 'name' => $user], 600);
        $data = $memcacheClient->get("memcache_test_" . $id);

        return [
            'set' => $set,
            'data' => $data
        ];
    }

    public function redis()
    {
        $user = $this->request->post('name', 'Hyperf');
        $id = random_int(1, 1000);

        // $redisClient = RedisClient::getInstance();
        // $tt = $redisClient->set('name:'.$id, $user, 600);
        // $data = $redisClient->get('name:'.$id);
        $tt = (RedisClient::getInstance())->lPush("queue:auto_ad_return_queue", json_encode(['tuser' => $user]));
        // $data = $redisClient->rPop("queue:auto_ad_return_queue");
        return [$tt];

    }

    public function mongo()
    {
        $user = $this->request->post('name', 'Hyperf');
        $id = random_int(1, 1000);

        $tt = MongoDBClient::getInstance()->set('name:' . $id, $user, 600);
        $data = MongoDBClient::getInstance()->get('name:' . $id);
        return [$tt, $data];
    }

}